package com.tcm.prescribe.service;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import com.base.core.dozer.DozerConvert;
import com.base.core.exception.ServiceException;
import com.base.core.pager.PageBean;
import com.base.core.pager.PageContext;
import com.base.core.utils.R;
import com.base.genid.generator.UidGenerator;
import com.tcm.prescribe.api.domain.dto.PrescribePhotoDTO;
import com.tcm.prescribe.api.domain.entity.PrescribePhoto;
import com.tcm.prescribe.api.domain.enums.BusinessCodeEnum;
import com.tcm.prescribe.api.service.PrescribePhotoService;
import com.tcm.prescribe.mapper.PrescribePhotoMapper;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

@DubboService(version = "0.0.1-alpha", retries = 3, timeout = 6000)
public class PrescribePhotoServiceImpl implements PrescribePhotoService {

    @Autowired
    private PrescribePhotoMapper prescribePhotoMapper;


    @DubboReference(version = "0.0.1-beta", group = "cached-uid", timeout = 6000, check = false, retries = 3)
    private UidGenerator uidGenerator;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void save(PrescribePhotoDTO prescribePhotoDTO) throws ServiceException {
        if (ObjectUtil.isEmpty(prescribePhotoDTO.getUserId())) {
            R.throwFail(BusinessCodeEnum.USER_ID_NOT_EXISTS);
        }
        if (ObjectUtil.isEmpty(prescribePhotoDTO.getPrescription())) {
            R.throwFail(BusinessCodeEnum.PLAN_PHOTO_NOT_EXISTS);
        }
        PrescribePhoto prescribePhoto = DozerConvert.copyProperties(prescribePhotoDTO, PrescribePhoto.class);
        prescribePhoto.setId(uidGenerator.getUID());
        prescribePhoto.setCreateTime(DateUtil.date());
        prescribePhoto.setUpdateTime(DateUtil.date());
        prescribePhoto.setStatus(new Byte("0"));
        prescribePhoto.setDeleted(new Byte("0"));
        prescribePhotoMapper.insert(prescribePhoto);
    }

    @Transactional
    @Override
    public void update(PrescribePhotoDTO treatmentPlanDto) throws ServiceException {

        if (ObjectUtil.isEmpty(treatmentPlanDto.getPrescription())) {
            R.throwFail(BusinessCodeEnum.PLAN_PHOTO_NOT_EXISTS);
        }
        if (ObjectUtil.isEmpty(treatmentPlanDto.getId())) {
            R.throwFail(BusinessCodeEnum.PLAN_PHOTO_ID_NOT_EXISTS);
        }
        PrescribePhoto treatmentPlan = DozerConvert.copyProperties(treatmentPlanDto, PrescribePhoto.class);
        treatmentPlan.setUpdateTime(DateUtil.date());
        prescribePhotoMapper.update(treatmentPlan);
    }

    /**
     * 方案列表
     *
     * @return
     */
    @Override
    public PageBean<PrescribePhotoDTO> listPageByUser(Long userId, Long page, Long pagesize) {

        if (ObjectUtil.isEmpty(userId)) {
            R.throwFail(BusinessCodeEnum.USER_ID_NOT_EXISTS);
        }

        Long count = prescribePhotoMapper.countPhotoByUser(userId);
        PageContext.setCurrentPage(page);
        PageContext.setPageSize(pagesize);
        PageContext.setDataCount(count);
        if(count == 0){
            return new PageBean<>(null);
        }
        List<PrescribePhoto> prescribePhotos = prescribePhotoMapper.listPhotoPageByUser(userId,PageContext.getStart(), pagesize);
        List<PrescribePhotoDTO> prescribePhotoDTOS = prescribePhotos.stream()
                .map(prescribePhoto -> {
                    PrescribePhotoDTO prescribePhotoDTO = new PrescribePhotoDTO();
                    BeanUtils.copyProperties(prescribePhoto, prescribePhotoDTO);
                    return prescribePhotoDTO;
                }).collect(Collectors.toList());
        return new PageBean<>(prescribePhotoDTOS);
    }

    /**
     * 医生端拍方列表
     *
     * @return
     */
    @Override
    public PageBean<PrescribePhotoDTO> listPageByDoctor(Long doctorId, Long page, Long pagesize) throws ServiceException {

        if (ObjectUtil.isEmpty(doctorId)) {
            R.throwFail(BusinessCodeEnum.DOCTOR_ID_NOT_EXISTS);
        }
        Long count = prescribePhotoMapper.countPhotoByDoctor(doctorId);
        PageContext.setCurrentPage(page);
        PageContext.setPageSize(pagesize);
        PageContext.setDataCount(count);
        if(count == 0){
            return new PageBean<>(null);
        }
        List<PrescribePhoto> prescribePhotos = prescribePhotoMapper.listPhotoPageByDoctor(doctorId,PageContext.getStart(), pagesize);
        List<PrescribePhotoDTO> prescribePhotoDTOS = prescribePhotos.stream()
                .map(prescribePhoto -> {
                    PrescribePhotoDTO prescribePhotoDTO = new PrescribePhotoDTO();
                    BeanUtils.copyProperties(prescribePhoto, prescribePhotoDTO);
                    return prescribePhotoDTO;
                }).collect(Collectors.toList());
       return new PageBean<>(prescribePhotoDTOS);
    }

    @Override
    public int deleteLogicById(Long id) {
        return prescribePhotoMapper.deleteLogicById(id);
    }

    @Override
    public int deleteById(Long id) {
        return prescribePhotoMapper.deleteById(id);
    }

    @Override
    public PrescribePhotoDTO findById(Long planId) {
        PrescribePhoto prescribePhoto = prescribePhotoMapper.findById(planId);
        PrescribePhotoDTO prescribePhotoDTO = null;
        if (ObjectUtil.isNotEmpty(prescribePhoto)) {
            prescribePhotoDTO = DozerConvert.copyProperties(prescribePhoto,PrescribePhotoDTO.class);
        }
        return prescribePhotoDTO;
    }
}
